package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.dtos.WmChannelPageReqDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmNewsService wmNewsService;


    /**
     * 查询文章列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmChannelPageReqDto dto) {
        //参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        //构建分页条件构造器
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> wrapper = Wrappers.<WmChannel>lambdaQuery();
        //2.2、频道名称模糊查询
        if (dto.getName() != null) {
            wrapper.like(WmChannel::getName, dto.getName());
        }
        //2.3、创建时间倒序

        wrapper.orderByDesc(WmChannel::getCreatedTime);

        //2.4、状态精确查询
        wrapper.eq(dto.getStatus() != null, WmChannel::getStatus, dto.getStatus());

        //查询数据库
        page(page, wrapper);

        //封装结果返回
        PageResponseResult result = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        result.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());

        return result;
    }

    /**
     * 新增频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {

        //参数校验
        if (wmChannel == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //频道名字不能重复

        WmChannel wc = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if (wc != null) {
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }
        //设置默认状态
        wmChannel.setIsDefault(true);
        //创建时间
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 修改文章
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannelDto dto) {
        //1、参数校验
        //开启不可修改
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、判断频道是否被引用（查询wmNews表），被引用不能禁用
        WmNews wmNews = wmNewsService.getOne(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, dto.getId()));
        //该频道下有文章
        if (wmNews != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        //3、修改文章频道状态
        update(Wrappers.<WmChannel>lambdaUpdate().eq(WmChannel::getId, dto.getId()).set(WmChannel::getStatus, dto.getStatus()));
        //4、修改频道其他信息

        update(Wrappers.<WmChannel>lambdaUpdate().eq(WmChannel::getId, dto.getId())
                .set(WmChannel::getName, dto.getName())
                .set(WmChannel::getDescription, dto.getDescription())
                .set(WmChannel::getOrd, dto.getOrd()));
        //4、返回响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        //1、参数校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.启动的频道不能删除
        WmChannel wmChannel = getById(id);
        if (wmChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "启用的频道不能删除");
        }
        //删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
